21. Airflow Hooks

13-1 - Building Pipelines In Airflow - Hooks And Connections - Demo 4

Connection via Airflow Hooks

Connections can be accessed in code via hooks. Hooks provide a reusable interface to external systems and databases. With hooks, you don’t have to worry about how and where to store these connection strings and secrets in your code.

from airflow import DAG
from airflow.hooks.postgres_hook import PostgresHook
from airflow.operators.python_operator import PythonOperator

def load():
# Create a PostgresHook option using the `demo` connection
    db_hook = PostgresHook(‘demo’)
    df = db_hook.get_pandas_df('SELECT * FROM rides')
    print(f'Successfully used PostgresHook to return {len(df)} records')

load_task = PythonOperator(task_id=’load’, python_callable=hello_world, ...)

Airflow comes with many Hooks that can integrate with common systems. Here are a few common ones:

  • HttpHook
  • PostgresHook (works with RedShift)
  • MySqlHook
  • SlackHook
  • PrestoHook